import { Module } from 'vuex';
import type { IRootState, ISystemState } from '@/store/types';
import {
  getPageListData,
  deletePageDataById,
  createPageData,
  editPageData,
} from '@/service/main/system/system';

const pageUrlMap = {
  user: '/users/list',
  role: '/role/list',
  goods: '/goods/list',
  menu: '/menu/list',
};

const queryInfoMap = {};

const systemModule: Module<ISystemState, IRootState> = {
  namespaced: true,

  state: {
    userList: [],
    userCount: 0,

    roleList: [],
    roleCount: 0,

    goodsList: [],
    goodsCount: 0,

    menuList: [],
    menuCount: 0,
  },

  mutations: {
    changeUserList(state, userList: any[]) {
      state.userList = userList;
    },

    changeRoleCount(state, roleCount: number) {
      state.roleCount = roleCount;
    },

    changeRoleList(state, roleList: any[]) {
      state.roleList = roleList;
    },

    changeUserCount(state, userCount: number) {
      state.userCount = userCount;
    },

    changeGoodsList(state, goodsList: any[]) {
      state.goodsList = goodsList;
    },

    changeGoodsCount(state, goodsCount: number) {
      state.goodsCount = goodsCount;
    },

    changeMenuList(state, menuList: any[]) {
      state.menuList = menuList;
    },

    changeMenuCount(state, menuCount: number) {
      state.menuCount = menuCount;
    },

    showListLoading(state, { id, pageName }) {
      const list = state[`${pageName}List`];
      if (list) {
        list.find((row) => row.id === id).isMyDeleteLoading = true;
      }
    },

    closeListLoading(state, { id, pageName }) {
      const list = state[`${pageName}List`];
      if (list) {
        list.find((row) => row.id === id).isMyDeleteLoading = false;
      }
    },
  },

  actions: {
    async getPageListAction({ commit }, payload: any) {
      const pageName = payload.pageName as string;
      const pageUrl = pageUrlMap[pageName];
      const res = await getPageListData(pageUrl, payload.queryInfo);
      const { list, totalCount } = res.data;
      const capitalUpperCase = pageName.toCapitalUpperCase();

      queryInfoMap[pageName] = payload.queryInfo;

      (list as Array<any>).map((o) => {
        o.isMyDeleteLoading = false;
      });
      commit(`change${capitalUpperCase}List`, list);
      commit(`change${capitalUpperCase}Count`, totalCount ?? 0);
    },

    async deletePageData(ctx, payload: { pageName: string; id: number }) {
      const { pageName, id } = payload;

      const pageUrl = `/${pageName}/${id}`;
      ctx.commit('showListLoading', { id, pageName });
      await deletePageDataById(pageUrl)
        .then(() => {
          ctx.dispatch('getPageListAction', {
            pageName,
            queryInfo: queryInfoMap[pageName],
          });
        })
        .catch(() => {
          setTimeout(() => {
            ctx.commit('closeListLoading', { id, pageName });
          }, 200);
        });
    },

    async createPageDataAction({ dispatch }, payload: any) {
      const { pageName, newData } = payload;
      const url = `/${pageName}`;

      await createPageData(url, newData);
      await dispatch('getPageListAction', {
        pageName,
        queryInfo: queryInfoMap[pageName],
      });
    },

    async editPageDataAction({ dispatch }, payload) {
      const { pageName, editData, id } = payload;

      const pageUrl = `/${pageName}/${id}`;

      await editPageData(pageUrl, editData);
      await dispatch('getPageListAction', {
        pageName,
        queryInfo: queryInfoMap[pageName],
      });
    },
  },

  getters: {
    pageListData(state) {
      return (pageName: string) => (state as any)[pageName + 'List'];
    },

    pageListCounter(state) {
      return (pageName: string) => (state as any)[pageName + 'Count'];
    },
  },
};

export default systemModule;
